//
//  AddPersonalLabelViewController.m
//  SouthChina
//
//  Created by kc on 15/7/8.
//  Copyright (c) 2015年 科创. All rights reserved.
//

#import "AddPersonalLabelViewController.h"
#import "QRadioButton.h"
#import "selectMemberTableViewCell.h"
@interface AddPersonalLabelViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *personallabel;         //个人标签
    NSMutableArray *personallabelold;         //之前个人标签
}
@property (strong, nonatomic) IBOutlet UITableView *tabview;
@property (strong, nonatomic) IBOutlet UITextField *userlabel;

@property (strong, nonatomic) IBOutlet UIButton *btAddLabel;

@end

@implementation AddPersonalLabelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   [self addTitleBarWithBackBtn:@"添加标签" needReturn:YES];
    
    //添加标签初始化
    personallabelold = [[NSMutableArray alloc]init];
    personallabel = [[NSMutableArray alloc]init];
    
    UIView * viewBody = [[[NSBundle mainBundle] loadNibNamed:@"AddPersonalLabelViewBody" owner:self options:nil] objectAtIndex:0];
    viewBody.frame = (CGRect){0, self.titlebar.frame.origin.y+self.titlebar.frame.size.height,SCREEN_WIDTH, SCREEN_HEIGHT-self.titlebar.frame.size.height};
    [self.view addSubview:viewBody];
    
    //读取个人标签
    NSString *sinittags = [DE getGlobalVar:LOGIN_USER_TAGS] == nil?@"":[DE getGlobalVar:LOGIN_USER_TAGS];
    NSLog(@"读取初始标签：%@",sinittags);
    if (![sinittags isEqualToString:@""]) {
        NSArray *list=[sinittags componentsSeparatedByString:@"，"];
        for(NSInteger i = 0;i<list.count;i++)
        {
            [personallabelold addObject: list[i]];//增加子定义标签
            [personallabel addObject: list[i]];//增加子定义标签(实时的)
        }
    }
   
    _tabview.dataSource = self;
    _tabview.delegate = self;
    
   
    _userlabel.placeholder = @"自定义标签";
    _userlabel.clearsOnBeginEditing = YES;    //再次编辑就清空
    _userlabel.keyboardType = UIKeyboardTypeNumberPad;   //设置键盘的样式
    
    //添加按钮增加边框
    [_btAddLabel.layer setMasksToBounds:YES];//设置按钮的圆角半径不会被遮挡
    [_btAddLabel.layer setBorderWidth:1];//设置边界的宽度
    //设置按钮的边界颜色
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    
   
    CGColorRef color = CGColorCreate(colorSpaceRef, (CGFloat[]){0,0.1,0.1,1});
    [_btAddLabel.layer setBorderColor:color];
    
    _tabview.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//相应方法：
-(void)didSelectedRadioButton:(QRadioButton *)radio groupId:(NSString *)groupId
{
   
}

//提交新加标签
- (IBAction)btClickAddLabel:(id)sender {
    
    //设置个人标签最多10个
    /*NSString *sinittags = [DE getGlobalVar:LOGIN_USER_TAGS] == nil?@"":[DE getGlobalVar:LOGIN_USER_TAGS];
    NSLog(@"读取初始标签：%@",sinittags);
    NSArray *list=[sinittags componentsSeparatedByString:@"，"];
    NSLog(@"标签初始化大小：%lu",[list count]);
    if ([list count]>=10) {
        [self showAlerHUD:@"你的个人标签已经达到了最多为6个的限制"];
        return;
    }
    //先清空所有对象
    //[personallabel removeAllObjects];
    //增加标签
    if(_userlabel.text.length == 0 )
    {
        NSLog(@"增加为空");
    }
    else{
         if([_userlabel.text isEqualToString:@"自定义标签"])
              NSLog(@"增加自定义标签");
         else
         {
             NSLog(@"增加标签%@",_userlabel.text);
             [personallabel addObject: _userlabel.text];//增加子定义标签
         }
        
    }
    
    //增加单选标签
    //[personallabel addObject: checklabel];
    
    //标签格式化入库
    NSString *stagslist=@"";
    for(NSInteger i = 0;i<personallabel.count;i++)
    {
        NSLog(@"用户选择和自定义标签为%@",[personallabel objectAtIndex:i] );
        if(i != (personallabel.count-1)) //追加,隔开
        {
           stagslist = [stagslist stringByAppendingString:[[personallabel objectAtIndex:i] stringByAppendingString:@"，"]];
        }
        else
        {
            stagslist = [stagslist stringByAppendingString:[personallabel objectAtIndex:i]];
        }
    }
    NSLog(@"最终写入数据库的标签为：%@",stagslist);
    
    if(![sinittags isEqualToString:@""])
    {
        sinittags = [@"，" stringByAppendingString:sinittags];
        stagslist = [stagslist stringByAppendingString:sinittags];
    }
    NSLog(@"最终写入数据库的标签为00：%@",stagslist);
   */
    NSString *stagslist=@"";
    NSDictionary *map = @{@"userTags":stagslist};
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [DE serverCall:@"updateUserInfo" params:map callback:^(BOOL success, int code, NSString *desc, NSDictionary *data,NSDictionary*param) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if(success)
        {
            [DE setGlobalVar:LOGIN_USER_TAGS value:stagslist];
            [self showAlerHUD:@"提交成功！"];
        }
        else{
            [self showAlerHUD:@"提交失败！"];
        }
    }];
}

//返回分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}



//每个分区多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return personallabelold.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 62;
}

//设置每行的显示
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"selectMemberTableViewCell";
    selectMemberTableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CellIdentifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:CellIdentifier owner:self options:nil] objectAtIndex:0];
    }
    cell.headImage.hidden = YES;
    cell.detailLabel.hidden = YES;
    cell.titlelconstraint.constant = -30;
    cell.titleLabel.font = [UIFont systemFontOfSize:18];
    cell.titleLabel.text = [personallabelold objectAtIndex:indexPath.row];
    
    BOOL bselected = NO;
    for(NSInteger i = 0;i<personallabel.count;i++)
    {
        if([[personallabelold objectAtIndex:indexPath.row] isEqualToString:[personallabel objectAtIndex:i]])
        {
            bselected = YES;
            break;
        }
        
    }
    [cell setContactSelected:bselected];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //选中单元格信息
    NSString *sLabel =[personallabelold objectAtIndex:indexPath.row];
    BOOL isFind = NO;
    for(NSInteger i = 0;i<personallabel.count;i++)
    {
        //已经选中过
        if([[personallabel objectAtIndex:i] isEqualToString:sLabel])
        {
            isFind = YES;
            [personallabel removeObjectAtIndex:i];
             NSLog(@"删除标签");
             break;
        }
     }
    
    //没有被选中
    if (isFind==NO) {
        [personallabel addObject:sLabel];
        {
             NSLog(@"增加标签");
        }
    }
    //刷新列表
    [_tabview reloadData];

}


@end
